package com.wechat.scheme.utils;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

//数组顺序重组
@Slf4j
public class ArrayRandomUtils {

    private static Random random = new Random();

    public static <T> void swap(T[] a, int i, int j) {
        T temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    public static <T> void shuffle(T[] arr) {
        int length = arr.length;
        for (int i = length; i > 0; i--) {
            int randInd = random.nextInt(i);
            swap(arr, randInd, i - 1);
        }
    }

    public static String array2IdStr(String[] TArr){
       return Arrays.asList(TArr).stream().collect(Collectors.joining(";"));
    }

    public static String list2IdStr(List<String> list,String patter){
        if(StringUtils.isEmpty(patter)){
            return list.stream().collect(Collectors.joining(";"));
        }
        return list.stream().collect(Collectors.joining(patter));
    }


    public static String randomQuesIds(String[] sourceArr) {
        ArrayRandomUtils.shuffle(sourceArr);
        String randomIdsStr = ArrayRandomUtils.array2IdStr(sourceArr);
        log.info("random done result str -> {}", randomIdsStr);
        return randomIdsStr;
    }


}
